﻿using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace program1
{
    public class LogRepository
    {
        private string connectionString;

        public LogRepository(string connectionString)
        {
            this.connectionString = connectionString;
        }

        public void LogAction(string action, string tableName, string details)
        {
            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                using (SQLiteCommand command = new SQLiteCommand(connection))
                {
                    command.CommandText = "INSERT INTO Logs (Timestamp, Action, TableName, Details) VALUES (@Timestamp, @Action, @TableName, @Details);";
                    command.Parameters.AddWithValue("@Timestamp", DateTime.Now);
                    command.Parameters.AddWithValue("@Action", action);
                    command.Parameters.AddWithValue("@TableName", tableName);
                    command.Parameters.AddWithValue("@Details", details);
                    command.ExecuteNonQuery();
                }
            }
        }

        public DataTable GetLogs()
        {
            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                using (SQLiteCommand command = new SQLiteCommand("SELECT * FROM Logs", connection))
                using (SQLiteDataAdapter adapter = new SQLiteDataAdapter(command))
                {
                    DataTable logsTable = new DataTable();
                    adapter.Fill(logsTable);
                    return logsTable;
                }
            }
        }
    }
}
